    /*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.JuniorDict;
import me.zhengjie.modules.biz.domain.req.JuniorDictListReq;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.biz.service.JuniorDictService;
import me.zhengjie.modules.biz.domain.vo.JuniorDictQueryCriteria;
import me.zhengjie.modules.biz.mapper.JuniorDictMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author yiwei.he
* @date 2024-06-18
**/
@Service
@RequiredArgsConstructor
public class JuniorDictServiceImpl extends ServiceImpl<JuniorDictMapper, JuniorDict> implements JuniorDictService {

    private final JuniorDictMapper juniorDictMapper;

    @Override
    public PageResult<JuniorDict> queryAll(JuniorDictQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(juniorDictMapper.findAll(criteria, page));
    }

    @Override
    public List<JuniorDict> queryAll(JuniorDictQueryCriteria criteria){
        return juniorDictMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(JuniorDict resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(JuniorDict resources) {
        JuniorDict juniorDict = getById(resources.getId());
        juniorDict.copy(resources);
        saveOrUpdate(juniorDict);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<JuniorDict> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (JuniorDict juniorDict : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("院校代码", juniorDict.getUcode());
            map.put("院校名称", juniorDict.getUname());
            map.put("专业代码", juniorDict.getPcode());
            map.put("专业名称", juniorDict.getPname());
            map.put("科类【1：文科 2：理科】", juniorDict.getSubjectType());
            map.put("最低分数线【省控线】", juniorDict.getMinScore());
            map.put("平均分数线", juniorDict.getAvgScore());
            map.put("最高分数线", juniorDict.getMaxScore());
            map.put("录取人数", juniorDict.getPlanNum());
            map.put("最低位次", juniorDict.getMinSort());
            map.put("平均位次", juniorDict.getAvgSort());
            map.put("最高位次", juniorDict.getMaxSort());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public PageResult<String> professionList(JuniorDictListReq req, Page<Object> page) {
        JuniorDictQueryCriteria criteria = new JuniorDictQueryCriteria();
        criteria.setSubjectType(req.getSubjectType());
        List<JuniorDict> all = juniorDictMapper.findAll(criteria);
        List<String> list = null;
        if(req.getType() == 1){
            list = all.stream().map(JuniorDict::getPname).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        }
        if(req.getType() == 2){
            list = all.stream().map(JuniorDict::getUname).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        }
        assert list != null;
        return PageUtil.toPage(PageUtil.paging((int) page.getCurrent() - 1, (int) page.getSize(), list),list.size());
    }
}
